package main

import (
	"TODecider/src/comnet"
	"TODecider/src/grpc"
	"TODecider/src/logger"
	"TODecider/src/to"
	"TODecider/src/util"
	"flag"
	"fmt"
)

var (
	cnaPort  int
	tsPort   int
	coldTime int
	mcIp     string
	algo     string
)

func init() {
	flag.IntVar(&cnaPort, "cna", 5555, "Port of Publisher of CNA (default 5555)")
	flag.IntVar(&tsPort, "ts", 6580, "Port of Server for TS (default 6580)")
	flag.IntVar(&coldTime, "cold", 50, "Cold time (ms) of TO concurrency (default 50)")
	flag.StringVar(&mcIp, "mc", "abc", "K3S Master node URL to obtain IP (default 'abc')")
	flag.StringVar(&algo, "algo", "km", "Matching algorithm type for TOD (['km', 'greedy', 'random'])")
}

func main() {
	flag.Parse()
	log := logger.GetLogger()
	// parse url
	hostIp := "0.0.0.0"
	if !util.IsLegalIP(mcIp) {
		log.Infof("[OS] input IP invalid, hostIp will be set as %s", hostIp)
	} else {
		hostIp = util.ObtainHostIPBy(fmt.Sprintf("http://%s:9090/my_ip", mcIp))
		log.Infof("[OS] hostIp is %s", hostIp)
	}
	to.InitAlgorithm(algo)
	go comnet.StartZmqSubscriber(fmt.Sprintf("%s:%d", hostIp, cnaPort))
	go grpc.StartGrpcServer(fmt.Sprintf("0.0.0.0:%d", tsPort), coldTime)
	select {}
}
